<template>
  <div class="home">
    <!-- 筛选 -->
    <div class="search_content">
      <van-nav-bar :title="$route.meta.title" left-text="返回" left-arrow @click-left="$router.go(-1)" />
      <form action>
        <van-search v-model="requestParams.numberOrBuy" placeholder="请输入销售合同编号或者买受人姓名" shape="round" @search="initData" @clear="clearSearch" />
      </form>
    </div>

    <!-- 列表 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="loadData" v-if="recordList && recordList.length != 0">
        <div class="contractList">
          <div class="contract_item" v-for="(item, index) in recordList" :key="index" @click="handleCheck(item, index)">
            <div class="contract_item_lt">
              <div class="txt">合同编号：{{ item.number }}</div>
              <div class="txt">买受人：{{ item.buyer }}</div>
              <div class="txt">车型：{{ item.carModelName }}</div>
              <div class="txt">合同价格：{{ item.tatalSaleAmt }}</div>
            </div>
            <div class="contract_item_rt">
              <img class="no_sel" :src="
                  require(`@/assets/images/${
                    selIndex == index ? 'sel_check' : 'no_sel'
                  }.png`)
                " />

              <!-- <van-radio-group v-model="item.check">
                <van-radio name="1"></van-radio>
              </van-radio-group>-->
            </div>
          </div>
        </div>
      </van-list>
      <van-empty class="noContent" description="暂无数据" v-else />
    </van-pull-refresh>
    <div class="addBtn" v-if="recordList && recordList.length != 0">
      <div class="footerBtn orderItem_filebox" @click="handleSubmit">确认</div>
    </div>
  </div>
</template>

<script>
import { Search, Empty, Notify } from "vant";
import sellManageApi from "@/api/sellManageApi";

export default {
  name: "buyManage",
  components: {
    Search,
    Empty,
    Notify
  },
  data() {
    return {
      value: "",
      carCouponList: [
        { name: "是", code: 1, check: false },
        { name: "否", code: 0, check: false }
      ],
      saleTypeList: [],
      statusList: [
        { name: "已处理", code: 1, check: false },
        { name: "未处理", code: 2, check: false }
      ],

      recordList: [
        // {
        //   billId: "1", //单据id
        //   number: "1231231", //编码
        //   buyer: "张三", //乙方买受人
        //   tatalSaleAmt: "1000000", //合同价
        //   carModel: "", //车型
        // },
        // {
        //   billId: "2", //单据id
        //   number: "1231231", //编码
        //   buyer: "张三", //乙方买受人
        //   tatalSaleAmt: "1000000", //合同价
        //   carModel: "", //车型
        // },
        // {
        //   billId: "3", //单据id
        //   number: "1231231", //编码
        //   buyer: "张三", //乙方买受人
        //   tatalSaleAmt: "1000000", //合同价
        //   carModel: "", //车型
        // },
      ],
      requestParams: {
        method: "getSaleContractByPurContract", //固定值
        pageNo: 1, //页码，非必填，String
        billId: "", //采购合同单据id
        numberOrBuy: "" //合同编码或者购买人查询条件
      },

      list: [],
      // 分页参数
      loading: false,
      finished: false,
      refreshing: false,
      isLoadData: false,
      selIndex: null,
      saleContractId: ""
    };
  },

  mounted() {
    if (this.$route.query) {
      this.requestParams.billId = this.$route.query.billId;
      this.initData();
    }
  },

  methods: {
    // 初始化列表
    initData() {
      this.requestParams.pageNo = 1;
      this.loadData();
    },
    loadData() {
      if (this.isLoadData) return;
      this.isLoadData = true;
      this.loading = true;
      this.finished = false;
      // console.log(this.isLoadData);
      sellManageApi
        .secondHandCarFacade({
          ...this.requestParams,
          method: "getSaleContractByPurContract"
        })
        .then(res => {
          if (this.requestParams.pageNo == 1) {
            this.recordList = res.data || [];
          } else {
            this.recordList = this.recordList.concat(res.data || []);
          }
          this.requestParams.pageNo += 1;

          if (res.data && res.data.length < 10) {
            this.finished = true;
            this.isLoadData = true;
          }

          this.isLoadData = false;
          this.loading = false;
        });
    },
    // 下拉刷新
    onRefresh() {
      // 清空列表数据
      this.recordList = [];
      this.finished = false;
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.refreshing = false;
      this.initData();
    },
    clearSearch() {
      this.requestParams.numberOrBuy = "";
      this.initData();
    },
    handleCheck(data, index) {
      console.log(data);
      this.selIndex = index;
      this.saleContractId = data.billId;
    },
    handleSubmit() {
      if (!this.saleContractId) {
        Notify({
          type: "danger",
          message: "请选择关联销售合同"
        });
        return;
      }
      sellManageApi
        .secondHandCarFacade({
          billId: this.requestParams.billId,
          method: "relaGenSaleContrat",
          saleContractId: this.saleContractId
        })
        .then(res => {
          if (res.code == 1) {
            Notify({
              type: "success",
              message: "关联成功"
            });
            this.$router.go(-1);
          }
        });
    }
  }
};
</script>

<style lang="less" scoped="">
body {
  background: #f3f3f3;
}
.search_content {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 2;
}
.timebox {
  /deep/.van-cell::after {
    border: none;
  }
  .time_item {
    /deep/.van-field__control--right {
      border: 1px solid #d1d1d1;
      padding: 5px 0;
      text-align: center;
      border-radius: 3px;
    }
  }
}
.full {
  width: 100% !important;
}
.searchbox {
  padding: 15px;

  .title {
    font-size: 15px;
    font-weight: bold;
    color: #333;
    margin-bottom: 10px;
    padding-top: 10px;
  }
  .insureList {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
    .insure_item {
      border: 1px solid #999;
      text-align: center;
      padding: 7px 0rem;
      width: 30%;
      box-sizing: border-box;
      // margin-right: 5px;
      font-size: 13px;
      border-radius: 3px;
      color: #666;
      margin-bottom: 10px;
      margin-right: 5%;
      &:nth-child(3n) {
        margin-right: 0;
      }
      &.active {
        border: 1px solid #f65e07;
        color: #f65e07;
      }
    }
  }
  .insureBtn {
    display: flex;
    justify-content: right;
    margin-top: 20px;
    .resetBtn {
      padding: 0 20px;
      margin-right: 10px;
    }
    .confirmBtn {
      padding: 0 20px;
    }
  }
}

.carList {
  padding-top: 148px;
  margin: 10px;
  overflow: hidden;
  .carItem {
    padding: 15px;
    background: #fff;
    border-radius: 10px;
    margin-bottom: 15px;
    .carItem_name {
      margin-bottom: 15px;
      padding-bottom: 10px;
      border-bottom: 1px solid #f6f6f6;
      position: relative;
      font-size: 14px;
      font-weight: bold;
      line-height: 15px;
    }
    .carItem_con {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      align-content: center;
      align-self: center;
      .carItem_con_lt {
        width: 80px;
        height: 80px;
        margin-right: 10px;
        img {
          width: 100%;
          height: 100%;
          display: block;
          border-radius: 5px;
        }
        .noimg {
          text-align: center;
          width: 80px;
          height: 80px;
          line-height: 80px;
          border-radius: 5px;
          // border: 1px solid #ccc;
          color: #999;
          font-size: 13px;
          background: #f4f4f4;
        }
      }
      .carItem_con_rt {
        .carItem_data {
          display: flex;
          align-items: center;
          font-size: 13px;
          margin-bottom: 5px;
          &:last-child {
            margin-bottom: 0;
          }
          .carItem_data_txt {
            border-right: 1px solid #999;
            color: #333;
            padding-right: 5px;
            margin-right: 5px;
            &:last-child {
              border-right: none;
            }
          }
          .carItem_label {
            color: #333;
          }
          .carItem_txt {
            color: #333;
          }
        }
      }
    }
    .carItem_footer {
      display: flex;
      justify-content: space-between;
      border-top: 1px solid #f6f6f6;
      margin-top: 5px;
      padding-top: 10px;
      align-items: center;
      .acount {
        font-size: 13px;
        color: #333;
        .txt {
          color: #e31f1f;
        }
        .txt1 {
          color: #666;
        }
      }
      .carStatus {
        background: #35bc04;
        color: #fff;
        border-radius: 3px;
        padding: 5px 7px;
        font-size: 12px;
      }
    }
  }
}

.noContent {
  margin-top: 2rem;
}
.contractList {
  padding-top: 100px;
  margin: 10px;
  overflow: hidden;
  .contract_item {
    padding: 15px;
    background: #fff;
    border-radius: 10px;
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .contract_item_lt {
      font-size: 15px;
      color: #333;
      .txt {
        font-size: 15px;
        margin-bottom: 5px;
      }
    }
    .contract_item_rt {
      .no_sel {
        width: 20px;
        height: 20px;
      }
    }
  }
}
.addBtn {
  position: fixed;
  bottom: 0.8rem;
  width: 100%;
  z-index: 1;
  .footerBtn {
    width: 80%;
    margin: auto;
    text-align: center;
    font-size: 14px;
    padding: 12px 0rem;
    border-radius: 10px;
  }
  .orderItem_filebox {
    margin: auto;
    background: #409eff;
    color: #fff;
  }
  .comfirmBtn {
    background: #409eff;
    color: #fff;
  }
}
</style>
